(function () {
    function Director() {
        Director.super(this);

        this.sceneWrap = null;
        this.alertWrap = null;
        this.loadScene = null;

        this.curScene = null;
        this.curAlert = null;

        this.alertMask = null;

        this.init();
    }
    Laya.class(Director, "Libs.Director", Laya.Sprite);
    var _proto = Director.prototype;
    _proto.createWrap = function () {
        var wrap = new Laya.Sprite();
            wrap.size(GAME_CONFIG.WIDTH, Laya.stage.height)

        return wrap;
    }
    _proto.addScene = function (scene) {
        ppt.header && ppt.header.exit();
        this.sceneWrap.addChild(scene);
        scene.onAdd && scene.onAdd();
		this.sceneWrap.addChild(ppt.header);

        this.curScene && this.curScene.onExit && this.curScene.onExit();
        (this.curAlert && this.curAlert.close && this.curAlert.close()) || (this.curAlert && this.curAlert.destroy && this.curAlert.destroy(true));

        this.curScene = scene;
    }
    _proto.curSceneEnter = function () {
        this.curScene.onEnter && this.curScene.onEnter();
    }

    _proto.init = function () {
        this.size(GAME_CONFIG.WIDTH, Laya.stage.height);

        this.sceneWrap = this.createWrap();
        this.alertWrap = this.createWrap();

        this.alertWrap.visible = false;

        this.addChildren(this.sceneWrap, this.alertWrap);
    }

    _proto.onResize = function (height) {
        console.log("Director onResize");
        this.height = height;
        this.sceneWrap.height = height;
        this.alertWrap.height = height;

        this.alertMask && (this.alertMask.centerY = 0);

        this.loadScene && this.loadScene.onResize(height);
        (this.curAlert && (this.curAlert.onResize && this.curAlert.onResize(height))) || (this.curAlert && (this.curAlert.y = height / 2 - this.curAlert.height / 2));
        this.curScene && this.curScene.onResize && this.curScene.onResize(height);
    }
    _proto.runScene = function (scene) {
        if(scene.ASSETS){
            if(!this.loadScene){
                this.loadScene = new Libs.LoadScene();
                this.addChild(this.loadScene);
            }
            this.addChild(this.loadScene);
            this.loadScene.load(scene.ASSETS, this.addScene.bind(this, scene), this.curSceneEnter.bind(this));
        }else{
            this.addScene(scene);
            this.curSceneEnter();
        }
    }
    _proto.popScene = function (scene, config) {
        if(this.curAlert){
            this.curAlert.offAll(Laya.Event.REMOVED);
            (this.curAlert.close && this.curAlert.close()) || this.curAlert.destroy(true);
        }

        if(scene.isCenter != false){
            scene.pos(GAME_CONFIG.WIDTH / 2 - scene.width / 2, Laya.stage.height / 2 - scene.height / 2);
        }

        if(!this.alertMask){
            var alertMask = new Laya.Image("res/public/mask.png");
                alertMask.alpha = 0.5;
                alertMask.size(1334, 1334);
                alertMask.centerX = 0;
                alertMask.centerY = 0;
                alertMask.mouseEnabled = true;

            this.alertMask = alertMask;
            this.alertWrap.addChild(alertMask);
        }
        this.alertMask.alpha = 0.5;
        this.alertWrap.visible = true;
        if(config){
            if(config.shadowClose){
                this.alertMask.once(Laya.Event.CLICK, scene, scene.close || scene.destroy);
            }
            if(config.shadowAlpha){
                this.alertMask.alpha = config.shadowAlpha;
            }
            if(config.autoClose){
                Laya.timer.once(config.autoClose, scene, scene.close || scene.destroy);
            }
        }
        this.alertWrap.addChild(scene);
        this.curAlert = scene;
        this.curAlert.once(Laya.Event.REMOVED, this, function () {
            console.log("CurAlert REMOVED");
            this.alertWrap.visible = false;
        });
    }
})();